package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.RoleService;
import com.erpanderp.wam.adm.model.entity.RoleServiceGroup;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.adm.model.service.ServiceGroupDAO;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class ServiceGroupsBean implements Serializable {
private static final long serialVersionUID = 1L;

private Map<String, Long> modulesMap = new HashMap<String, Long>();
private Map<String, Long> serviceGroupsMap = new HashMap<String, Long>();

ModuleDAO moduleDAO = new ModuleDAO();
ServiceGroupDAO serviceGroupDAO = new ServiceGroupDAO();
ServiceDAO serviceDAO = new ServiceDAO();



private List<Module> modules = new ArrayList<Module>();
private List<ServiceGroup> serviceGroups = new ArrayList<ServiceGroup>();
private List<Service> services = new ArrayList<Service>();
private List<Service> assignedServices = new ArrayList<Service>();



private ServiceGroup serviceGroup = new ServiceGroup();
private Service service= new Service();

private Long selectedModuleId;
private Long selectedServiceGroupId;
private ServiceGroup selectedServiceGroup;
private Service servicesBySelectedServiceGroup;

private boolean detailsForServiceGroup;



public ServiceGroupsBean()
{
	setDetailsForServiceGroup(false);
	List<Module> modules = moduleDAO.getAllModules();
	for (Module module : modules) 
	{
		modulesMap.put(module.getModuleName(), module.getModuleId());
	}
}

public void moduleChangeListener(AjaxBehaviorEvent e) {
	serviceGroupsMap.clear();
	if(selectedModuleId == null || selectedModuleId == 0)
	{
		
	}
	else
	{
	serviceGroups = serviceGroupDAO.getServicegroupsByModuleId(selectedModuleId);
	for (ServiceGroup serviceGroup : serviceGroups) {
	serviceGroupsMap.put(serviceGroup.getServicegroupName(),
	serviceGroup.getServiceGroupId());
	}
	}
}

public void serviceGroupChangeListener(AjaxBehaviorEvent e1) {
	
	assignedServices.clear();
	System.out.println("serviceGroupChangeListener..........");
	if(selectedServiceGroupId == null || selectedServiceGroupId == 0)
	{
		setDetailsForServiceGroup(false);
	}
	else
	{
		selectedServiceGroup = serviceGroupDAO.getServiceGroupById(selectedServiceGroupId);
		if (selectedServiceGroup.getDescription() == null || selectedServiceGroup.getDescription().equals("")) 
		{
			selectedServiceGroup.setDescription("Enter Description");
		}
		System.out.println("selectedResource........"+selectedServiceGroup);
		if (services.size() == 0) 
		{
			services = serviceDAO.getAllServices();
			
		}
		
		assignedServices = serviceDAO.getAllServicesByServiceGroupID(selectedServiceGroupId);
		for(Service service : services)
		{
			if(assignedServices.contains(service))
			{
				service.setAssigned(true);
				service.setChecked(true);
			}else{
				service.setAssigned(false);
				service.setChecked(false);
			}
		}
		setDetailsForServiceGroup(true);
	}
	
}
public String updateServiceGroupAction() {	
	
	
	Date now = new Date();
	//assignedServiceGroups.clear();
	assignedServices.clear();
	
	for(Service service : services){			
		if(!service.isAssigned() && service.isChecked()){
			assignedServices.add(service);
			
			serviceGroup.setCreateDate(now);
			serviceGroup.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			//serviceGroup.sets(selectedRole);
			//serviceGroup.setService(service);
			serviceGroup.setStatus(CommonLookup.STATUS_ACTIVE);
			selectedServiceGroup.getServices().add(service);
			System.out.println("SERVICE ADDED");
		}else if(service.isAssigned() && !service.isChecked()){
			service.setServiceGroup(null);
			selectedServiceGroup.getServices().remove(service);
		}
		
	}	
	
	serviceGroupDAO.updateServiceGroup(selectedServiceGroup);
	
	return null;
}


//GETTERS AND SETTERS
public Service getServicesBySelectedServiceGroup() {
	return servicesBySelectedServiceGroup;
}

public void setServicesBySelectedServiceGroup(
		Service servicesBySelectedServiceGroup) {
	this.servicesBySelectedServiceGroup = servicesBySelectedServiceGroup;
}
public ModuleDAO getModuleDAO() {
	return moduleDAO;
}

public void setModuleDAO(ModuleDAO moduleDAO) {
	this.moduleDAO = moduleDAO;
}

public ServiceGroupDAO getServiceGroupDAO() {
	return serviceGroupDAO;
}

public void setServiceGroupDAO(ServiceGroupDAO serviceGroupDAO) {
	this.serviceGroupDAO = serviceGroupDAO;
}

public Map<String, Long> getModulesMap() {
	return modulesMap;
}

public void setModulesMap(Map<String, Long> modulesMap) {
	this.modulesMap = modulesMap;
}



public Map<String, Long> getServiceGroupsMap() {
	return serviceGroupsMap;
}

public void setServiceGroupsMap(Map<String, Long> serviceGroupsMap) {
	this.serviceGroupsMap = serviceGroupsMap;
}

public List<Module> getModules() {
	return modules;
}

public void setModules(List<Module> modules) {
	this.modules = modules;
}

public List<ServiceGroup> getServiceGroups() {
	return serviceGroups;
}

public void setServiceGroups(List<ServiceGroup> serviceGroups) {
	this.serviceGroups = serviceGroups;
}

public ServiceGroup getServiceGroup() {
	return serviceGroup;
}

public void setServiceGroup(ServiceGroup serviceGroup) {
	this.serviceGroup = serviceGroup;
}

public Long getSelectedModuleId() {
	return selectedModuleId;
}

public void setSelectedModuleId(Long selectedModuleId) {
	this.selectedModuleId = selectedModuleId;
}

public Long getSelectedServiceGroupId() {
	return selectedServiceGroupId;
}

public void setSelectedServiceGroupId(Long selectedServiceGroupId) {
	this.selectedServiceGroupId = selectedServiceGroupId;
}

public ServiceGroup getSelectedServiceGroup() {
	return selectedServiceGroup;
}

public void setSelectedServiceGroup(ServiceGroup selectedServiceGroup) {
	this.selectedServiceGroup = selectedServiceGroup;
}

public ServiceDAO getServiceDAO() {
	return serviceDAO;
}

public void setServiceDAO(ServiceDAO serviceDAO) {
	this.serviceDAO = serviceDAO;
}

public List<Service> getServices() {
	return services;
}

public void setServices(List<Service> services) {
	this.services = services;
}

public Service getService() {
	return service;
}

public void setService(Service service) {
	this.service = service;
}

public List<Service> getAssignedServices() {
	return assignedServices;
}

public void setAssignedServices(List<Service> assignedServices) {
	this.assignedServices = assignedServices;
}

public boolean isDetailsForServiceGroup() {
	return detailsForServiceGroup;
}

public void setDetailsForServiceGroup(boolean detailsForServiceGroup) {
	this.detailsForServiceGroup = detailsForServiceGroup;
}
}
